<template>
  <div class="user">
    <el-button type="primary" @click="dialogFormVisible = true">预约</el-button>
    <el-dialog title="预约详情" :visible.sync="dialogFormVisible">
      <el-form :model="form">
        <el-form-item label="姓名👨" :label-width="formLabelWidth">
          <el-input v-model="form.username" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="电话📞" :label-width="formLabelWidth">
          <el-input v-model="form.tel" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="editUser(form)">确 定</el-button>
      </div>
    </el-dialog>
    <el-table
      :data="userList"
      style="width: 100%"
      :row-class-name="tableRowClassName">
      <el-table-column
        prop="id"
        label="编号"
        width="180">
      </el-table-column>
      <el-table-column
        prop="username"
        label="姓名"
        width="180">
      </el-table-column>
      <el-table-column
        prop="tel"
        label="电话">
      </el-table-column>
      <el-table-column
        prop="date"
        label="时间">
      </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="primary"
            icon="el-icon-search"
            @click="handleDetail(scope.$index, scope.row)">详情</el-button>
          <el-button
            size="mini"
            type="warning"
            icon="el-icon-edit"
            @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
          <el-button
            size="mini"
            type="danger"
            icon="el-icon-delete"
            @click="handleDelete(scope.$index, scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      @current-change="getListInfo"
      @next-click="nextInfo"
      background
      layout="prev, pager, next"
      :total=totlelist>
    </el-pagination>
  </div>
</template>

<script>
import dayjs from 'dayjs'
  export default {
    name:"UserList",
    filters:{
      date(v){
        return dayjs(v).format('MM/DD')
      }
    },
    data() {
      return {
        userList:[],
        totlelist:null,
        dialogFormVisible: false,
        form: {
          username: '',
          tel:'',
        },
        formLabelWidth: '140px'
      }
    },
    watch: {
      dialogFormVisible:function(e){
        if (!e) {
          this.form={}
        }
      }
    },
    created() {
      this.getInfo(),
      this.$http.get('/api/userLength').then(res=>{
        this.totlelist=res.data
      })
    },
    methods: {
      date(v){
        return dayjs(v).format('MM/DD')
      },
      /* crrentPage(pageIndex){

      }, */
      //编辑用户
      async editUser(r){
        console.log(r._id);
        if (!r.id) {
          const res = await this.$http.put('/api/user',r)
          console.log(res.data);
          this.dialogFormVisible = false
          
        }
        const res = await this.$http.put(`/api/user/${r._id}`,r)
        console.log(res.data);
        this.getListInfo()
        this.dialogFormVisible = false
      },
      //获取当前页用户
      async getInfo(pageIndex=1,pageSize=10){
        const res = await this.$http.get('/api/userPage',{params:{
          pageIndex,
          pageSize
        }})
        this.userList=res.data
        this.userList.map(r=>{
          if (r.updatedAt) {
            return r.date=this.date(r.updatedAt)
          }
        })
      },
      getListInfo(pageIndex){
        console.log(pageIndex);
        this.getInfo(pageIndex)
      },
      nextInfo(i){
        console.log(i);
        
      },
      tableRowClassName({ rowIndex}) {
        if (rowIndex === 1) {
          return 'warning-row';
        } else if (rowIndex === 3) {
          return 'success-row';
        }
        return '';
      },
      //删除这一行
      async handleDetail(index, row){
        console.log(index, row);
        const res = await this.$http.delete(`/api/user/${row._id}`,row)
        console.log(res.data);
        
      },
      handleEdit(index, row) {
        console.log(index, row);
        this.dialogFormVisible=true
        this.form=row
      },
      handleDelete(index, row) {
        console.log(index, row);
        this.$confirm(`此操作将永久删除${row.username}, 是否继续?`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.$http.delete(`/api/user/${row._id}`,row)
          //console.log(res.data);
          this.$message({
            type: 'success',
            message: '删除成功!'
          });
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          });          
        });
      }
    },
  }
</script>

<style lang="scss" scoped>
.user{
  flex: 1;
  padding: 10px;
}

  .el-table .warning-row {
    background: oldlace;
  }
.el-dialog{
  width:39%;
}
  .el-form-item{
    margin-right: 100px;
  }
  .el-table .success-row {
    background: #f0f9eb;
  }
</style>